<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Db;

class Message extends Common
{
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['title']) && !empty($post['title'])){
            $where[] = ['title', 'like', '%'.$post['title'].'%'];
        }
        if(isset($post['desc']) && !empty($post['desc'])){
            $where[] = ['desc', 'like', '%'.$post['desc'].'%'];
        }
        $list = Db::name('message')
            ->where($where)
            ->order('is_top desc,ctime desc')
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    public function setAdd($data)
    {
        $data['title']      = trim($data['title']);
        $data['content']    = trim($data['content']);

        $data = [
            'title'     => trim($data['title']),
            'content'   => trim($data['content']),
            'ctime'     => getUtcTime()
        ];
        return Db::name('message')->save($data);
    }

    public function setEdit($data)
    {
        $set_id         = $data['set_id'];
        $update_param   = [
            'title'     => trim($data['title']),
            'content'   => trim($data['content']),
        ];
        return Db::name('message')
                ->where(['id'=>$set_id])
                ->update($update_param);
    }

    public function changeStatus($data)
    {
        $msg_id     = $data['msg_id'];
        $manage_id  = $data['manage_id'];

        $blog = true;
        Db::startTrans();
        try{
            $ctime          = getUtcTime();
            $current_list   = Db::name('message')->where(['id'=>$msg_id])->find();
            $current_top    = $current_list['is_top'];
            if($current_top == 1){
                //将本来置顶的改为0
                Db::name('message')->where(['id'=>$msg_id])->update(['is_top' => 0]);
                $new_top = 0;
            }else{
                //将已有的置顶修改为0
                $list = Db::name('message')->where(['is_top' => 1])->find();
                if(isset($list['id'])){
                    Db::name('message')->where(['id'=>$list['id']])->update(['is_top' => 0]);
                }
                //将需要置顶的改为1
                Db::name('message')->where(['id'=>$msg_id])->update(['is_top' => 1]);
                $new_top = 1;
            }
            ManageLog::saveManageLog([
                'manage_id' => $manage_id,
                'desc'      => "change Message Top",
                'content'   => [
                    'msg_id' => $msg_id,
                    'old_top'=> $current_top,
                    'new_top'=> $new_top,
                ],
                'ctime'     => $ctime
            ]);
            Db::commit();
        } catch (\Exception $e) {
            var_dump($e->getMessage());
            $blog       = false;
            Db::rollback();
        }
        return $blog;
    }
}
